package com.yuxl.common.core.domain;


import lombok.Data;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.util.MultiValueMap;


@Data
public class R<T> {

    private boolean success;
    private int status;
    private String message;
    private T data;

    public R() {
        this(true,null, null,HttpStatus.OK);
    }

    public R(HttpStatus status) {
        this(status.equals(HttpStatus.OK),null, null,status);
    }

    public R(@Nullable String msg) {
        this(false,null, msg,HttpStatus.INTERNAL_SERVER_ERROR);
    }

    public R(@Nullable String msg,HttpStatusCode status) {
        this(status.equals(HttpStatus.OK),null, msg,status);
    }

    public R(@Nullable T data) {
        this(true,data, null,HttpStatus.OK);
    }

    public R(boolean success,@Nullable T body,String message,HttpStatusCode status) {
        this.success = success;
        this.data = body;
        this.message = message;
        this.status = status.value();
    }

    // 以下方法直接返回R<T>对象，不包装为ResponseEntity
    public static R<Void> ok() {
        return new R<>();
    }
    public static <T> R<T> ok(T body) {
        return new R<>(body);
    }

    public static <T> R<T> error(String message) {
        return new R<>(message, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    public static <T> R<T> error(int status, String message) {
        return new R<>(message, HttpStatusCode.valueOf(status));
    }

    public static <T> R<T> error(HttpStatusCode status, String message) {
        return new R<>(message, status);
    }

}